package com.neusoft.utils;

import java.lang.reflect.Field;

/**
 * @Author: duan
 * @Date: Create in 15:20 2020/10/22
 */
public class ClassUtil {
    public static String generateInsertSqlFromObject(Object obj, String tableName) {
        Class cls = obj.getClass();
        Field[] fs = cls.getDeclaredFields();
        StringBuilder sb = new StringBuilder("insert into ");
        sb.append(tableName);
        sb.append("(");
        for (Field item : fs) {
            sb.append(item.getName());
            sb.append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(") values(");
        for (Field item : fs) {
            sb.append("?,");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(")");



        return sb.toString();
    }

    public static void main(String[] args) {
//        Student bean = new Student();
//        bean.setId(0)
//            .setDormitory("404")
//            .setPassword("123")
//            .setPhone("11111111111")
//            .setSex("0")
//            .setUsername("haha");
//        System.out.println(generateInsertSqlFromObject(bean, "student"));
    }
}
